<?php
use think\migration\Migration;

class PointsTables extends Migration
{
    public function change()
    {
        // 用户表增加积分字段
        if ($this->table('users')->hasColumn('points') === false) {
            $this->table('users')
                ->addColumn('points', 'integer', ['default' => 0, 'comment' => '积分余额'])
                ->update();
        }

        // 积分流水表
        if (!$this->hasTable('points_log')) {
            $this->table('points_log')
                ->addColumn('user_id', 'integer')
                ->addColumn('change', 'integer', ['comment' => '变动积分，正为增加，负为扣除'])
                ->addColumn('type', 'string', ['limit' => 20, 'comment' => '类型：recharge/consume/refund'])
                ->addColumn('remark', 'string', ['limit' => 255, 'default' => '', 'comment' => '备注'])
                ->addColumn('create_time', 'integer')
                ->create();
        }

        // 充值订单表
        if (!$this->hasTable('points_order')) {
            $this->table('points_order')
                ->addColumn('user_id', 'integer')
                ->addColumn('order_no', 'string', ['limit' => 64])
                ->addColumn('amount', 'decimal', ['precision' => 10, 'scale' => 2, 'comment' => '支付金额'])
                ->addColumn('points', 'integer', ['comment' => '充值积分'])
                ->addColumn('status', 'integer', ['default' => 0, 'comment' => '0待支付 1已支付 2已取消'])
                ->addColumn('pay_type', 'string', ['limit' => 20, 'default' => '', 'comment' => '支付方式'])
                ->addColumn('pay_time', 'integer', ['default' => 0])
                ->addColumn('create_time', 'integer')
                ->create();
        }
    }
} 